const JwtStrategy = require('passport-jwt').Strategy
const ExtractJwt = require('passport-jwt').ExtractJwt
// const mongoose = require('mongoose')
const User = require('../model/UserModel')
const keys = require('../config/keys')
const opts = {}
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken() // 通过配置信息来生成jwt的请求，验证这个token
opts.secretOrKey = keys.secretOrKey

module.exports = (passport) => {
  passport.use(
    new JwtStrategy(opts, function (payload, done) {
      User.findById(payload.id)
        .then((user) => {
          if (user) {
            return done(null, user)
          }
          return done(null, null)
        })
        .catch((err) => console.log(err))
    })
  )
}
